<?php
/**
 * Created by PhpStorm.
 * User: cyc
 * Date: 2017/11/1
 * Time: 下午2:53
 */

class ApiAdviceService extends ApiBaseService
{
    /**
     * @param $params
     *  page  | false    |int     |分页,默认为0
     * size| false    |int     |偏移量，默认为3
     */
    public static function freshtopicsAction($params)
    {
        $page = self::util_array_get($params, 'page', 0);
        $size = self::util_array_get($params, 'size', 3);
        $advice = Advice::model()->findByAttributes(['upload' => Advice::UPLOAD_UPLOAD, 'type' => Advice::ADVICE_TYPE_TOPIC]);
        $topics = array();
        if ($advice) {
            $exts = $advice->advice_exts;
            $tids = array_map(function (AdviceExt $ext) {
                return $ext->ext_id;
            }, $exts);
            $cdbcriteria = new CDbCriteria();
            $cdbcriteria->offset = $page;
            $cdbcriteria->limit = $size;
            $cdbcriteria->addInCondition('id', $tids);
            foreach (Topic::model()->findAll($cdbcriteria)as $topic)
            {
                $topics[]=['id'=>$topic->id,'name'=>$topic->name,'avater'=>$topic->getRealUrl()];
            }

        }
        return $topics;
    }

    public static function videoAction($params)
    {
        $page = self::util_array_get($params, 'page', 0);
        $size = self::util_array_get($params, 'size', 3);
        $advice = Advice::model()->findByAttributes(['upload' => Advice::UPLOAD_UPLOAD, 'type' => Advice::ADVICE_TYPE_CONTENT]);
        $adviceVideos = array();
        if ($advice) {
            $exts = $advice->advice_exts;
            $tids = array_map(function (AdviceExt $ext) {
                return $ext->ext_id;
            }, $exts);
            $cdbcriteria = new CDbCriteria();
            $cdbcriteria->offset = $page;
            $cdbcriteria->limit = $size;
            $cdbcriteria->addInCondition('id', $tids);
            foreach (Video::model()->findAll($cdbcriteria)as $video)
            {
                if($topics=$video->findTopics())
                {
                    $topic = $topics[0];
                    $topic =['t_id'=>$topic->id,'t_name'=>$topic->name,'t_avater'=>$topic->getRealUrl(), 'color'=>$topic->serieses[0]->color];
                    $video=['v_id'=>$video->id,'v_name'=>$video->name,'v_avater'=>$video->avetar];
                    $adviceVideos [] = array_merge($topic, $video);
                }
            }

        }
        return $adviceVideos;

    }





}